<?php

if( !defined( 'MEDIAWIKI' ) )
	die(-1);

require_once('DatabaseFunctions.php');

$wgExtensionCredits['specialpage'][] = array(
	'name' => 'BrowseTags',
	'author' => 'mp2p',
);


#install extension hook
$wgExtensionFunctions[] = "wfSpecialPageBrowseTags";

function wfSpecialPageBrowseTags()
{
	SpecialPage::addPage( new BrowseTags() );
}


class BrowseTags extends SpecialPage
{
	function BrowseTags(){
		SpecialPage::SpecialPage("BrowseTags");
		return true;
		
	}

	function execute() {
       		global $wgOut, $wgRequest;
		$wgOut->setDefaultTitle('Browse Tags');
		$wgOut->setPagetitle("Browse Tags");
		$this->paintTags();
	}
	
	function paintTags() 
	{
		global $wgMessageCache;
		global $wgRequest, $wgDBprefix, $wgOut, $wgUser, $IP;;
			
		$dbr = wfGetDB( DB_SLAVE );
	 
		$sql = "select 1 source, s.Count as w, t.Artist, t.Artist display from searches s inner join ss_tags t on s.idtags=t.idtags  ";
		$sql.= "where s.query='groupby=attr:artistkey;limit=100;select=idtags as idtags, sum((10/firstSeen)*weightTags*totalCount) as Count;groupsort=Count desc;sort=expr:1000/(CountFiles*weightTags+1);floatrange=weightTags,0.91,1;filter=type,1'";                              
		$sql.=" union select 2 source, s.Count as w, t.Artist, t.Album display ";
		$sql.=" from searches s inner join ss_tags t on s.idtags=t.idtags ";
		$sql.=" where s.query ='groupby=attr:topAlbumkey;offset=1; limit=100;select=idtags as idtags, sum(10/firstSeen*weightTags*totalCount) as Count;groupsort=Count desc;sort=expr:1000/(CountFiles*weightTags+1);floatrange=weightTags,0.91,1;filter=type,1' ";
		$sql .=" union select 3 source, s.Count as w, t.Artist, t.Title display";
		$sql.=" from searches s inner join ss_tags t on s.idtags=t.idtags ";
		$sql.=" where s.query ='groupby=attr:songKey;limit=100;select=idtags as idtags, sum(10/firstSeen*weightTags*totalCount) as Count;groupsort=Count desc;sort=expr:1000/(CountFiles*weightTags+1);floatrange=weightTags,0.91,1;filter=type,1'";
		$sql.=" order by 4;";
	  
		$res = $dbr->query( $sql );


		//insert tags in array from database
		while ($row = $dbr->fetchObject( $res )) {


		$total_weight[$row->source] += $row->w;
		$tags[] = $row->Artist.'@'.$row->w.'@'.$row->display.'@'.$row->source;
		}
		$dbr->freeResult ( $res );
	 
		//$wgOut->switchBocataMode(true);
		$wgOut->addHTML('<div id="tagcloudBrowsetags" class="tagcloud" ><ul>');

		  // Increasing this number will make the words bigger; Decreasing will do reverse
		$factor = 0.5;
		// Smallest font size possible
		$starting_font_size = 8;
		for ($i=1;$i<=3;$i++)
			$max_count[$i] = $total_weight[$i]/5;

		$ssSearchPath = '/search/';

		foreach ($tags as $key => $value)
		{

		$values = explode("@", $value);
		$x = round(($values[1] * 110) / $max_count[$values[3]]) * $factor;

		//fix the max
		if ($x > 18) {
		  $x = 18;
		}

		$font_size = $starting_font_size + $x.'px';
		$link="";
		switch($values[3]){
			case 1:
				$link="/music/".urlencode($values[0]);
				break;
			case 2:
				$link="/music/".urlencode($values[0])."/album/".urlencode($values[2]);
				break;
			case 3:
				$link="/download/".urlencode($values[0])."/".urlencode($values[2]);
				break;
		}
		$query_search = str_replace(' ', ' <wbr />',$values[2]);
		$values[2] = str_replace('/','',$values[2]); //ac/dc breaks	query
		

		$wgOut->addHTML('<li><a class="tag" style="font-size: '.$font_size.'" title="'.$values[2].'" href="'.$link.'">'.$query_search.'<wbr /></a></li>');
		
		/*var_dump($values);
		$j++;
		if ($j>2)
			die($value);
		*/
	  }

	  

	  $wgOut->addHTML('</ul></div>');  
	}
	
}
